package cn.sfrog.project.biz.web.service.impl;

import cn.sfrog.common.constant.Constants;
import cn.sfrog.common.core.domain.AjaxResult;
import cn.sfrog.common.core.domain.ApiAjaxResult;
import cn.sfrog.common.core.domain.entity.SysUser;
import cn.sfrog.common.core.domain.model.LoginUser;
import cn.sfrog.common.core.redis.RedisCache;
import cn.sfrog.common.enums.UserStatus;
import cn.sfrog.common.enums.UserTypeEnum;
import cn.sfrog.common.exception.ApiException;
import cn.sfrog.common.utils.DateUtils;
import cn.sfrog.common.utils.IdentificationCodeUtil;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.common.utils.StringUtils;
import cn.sfrog.framework.web.service.SysPermissionService;
import cn.sfrog.framework.web.service.TokenService;
import cn.sfrog.project.biz.admin.domain.*;
import cn.sfrog.project.biz.admin.mapper.BizLiveRoomMapper;
import cn.sfrog.project.biz.admin.mapper.BizUserAccountMapper;
import cn.sfrog.project.biz.admin.service.IBizUserAccountService;
import cn.sfrog.project.biz.admin.service.IBizUserInviteRecordService;
import cn.sfrog.project.biz.admin.service.IBizUserProfessionRecordService;
import cn.sfrog.project.biz.admin.tencent.TencentApiResponse;
import cn.sfrog.project.biz.admin.tencent.TencentLiveApiService;
import cn.sfrog.project.biz.admin.tencent.TencentLiveProperties;
import cn.sfrog.project.biz.admin.utils.PhoneFormatCheckUtils;
import cn.sfrog.project.biz.admin.utils.tencentlive.LiveUtils;
import cn.sfrog.project.biz.web.service.AppUserService;
import cn.sfrog.project.biz.web.vo.AppSubmitIdCardInfoVo;
import cn.sfrog.project.biz.web.vo.AppSubmitJobAuthFormVo;
import cn.sfrog.project.biz.web.vo.account.AppUserAuthStatusVO;
import cn.sfrog.project.biz.web.vo.index.AppDoctorProfessionalVO;
import cn.sfrog.system.domain.vo.app.*;
import cn.sfrog.system.mapper.SysUserMapper;
import cn.sfrog.system.service.ISysUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Slf4j
@Service
public class AppUserServiceImpl implements AppUserService {
    @Resource
    private ISysUserService userService;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private RedisCache redisCache;
    @Resource
    private SysPermissionService permissionService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private IBizUserAccountService bizUserAccountService;
    @Resource
    private BizUserAccountMapper bizUserAccountMapper;
    @Autowired
    private IBizUserInviteRecordService bizUserInviteRecordService;
    @Autowired
    private IBizUserProfessionRecordService bizUserProfessionRecordService;

    @Autowired
    private TencentLiveApiService liveApiService;

    @Autowired
    private TencentLiveProperties liveProperties;

    @Autowired
    private BizLiveRoomMapper liveRoomMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public Boolean appUserRegister(AppUserRegisterFormVO registerFormVO) {
        if (!PhoneFormatCheckUtils.isChinaPhoneLegal(registerFormVO.getPhoneNumber())) {
            throw new ApiException("手机号码有误,请重新输入");
        }
        if (!StringUtils.equals(Constants.DEFAULT_REGISTER_CODE, registerFormVO.getCode())) {
            Object cacheObject = redisCache.getCacheObject(Constants.REGISTER_CACHE_PHONE_CODE_KEY + registerFormVO.getPhoneNumber());
            if (cacheObject == null) {
                throw new ApiException("短信验证码已过期");
            }
            String code = String.valueOf(cacheObject);
            if (!StringUtils.equals(code, registerFormVO.getCode())) {
                throw new ApiException("短信验证码输入错误");
            }
            redisCache.deleteObject(Constants.REGISTER_CACHE_PHONE_CODE_KEY + registerFormVO.getPhoneNumber());
        }
        //判断注册的手机号和用户类型是否存在于系统中
        //int index = sysUserMapper.checkUserNameUnique(registerFormVO.getPhoneNumber());
        int index = 0;
        if ("1".equals(registerFormVO.getClientType())) {
            index = sysUserMapper.checkUserNameAndTypeUnique(registerFormVO.getPhoneNumber(), registerFormVO.getUserType());
        } else if ("2".equals(registerFormVO.getClientType())) {
            index = sysUserMapper.checkUserNameAndTypeUnique2(registerFormVO.getPhoneNumber());
        } else if ("3".equals(registerFormVO.getClientType())) {
            index = sysUserMapper.checkUserNameAndTypeUnique(registerFormVO.getPhoneNumber(), registerFormVO.getUserType());
        }

        if (index > 0) {
            throw new ApiException("该手机号已被注册");
        }
        SysUser sysUser = new SysUser();
        sysUser.setUserName(registerFormVO.getPhoneNumber());
        if (StringUtils.isNotBlank(registerFormVO.getNickName())) {
            sysUser.setNickName(registerFormVO.getNickName());
        } else {
            sysUser.setNickName(registerFormVO.getPhoneNumber());
        }
        sysUser.setPhonenumber(registerFormVO.getPhoneNumber());
        //  注册所有人都是会员用户 userTtpe=10
        sysUser.setUserType(registerFormVO.getUserType());
        if (StringUtils.isNotNull(registerFormVO.getInviteUserId())) {
            sysUser.setParentUserId(registerFormVO.getInviteUserId());
            SysUser parentUser = sysUserMapper.selectUserById(registerFormVO.getInviteUserId());
            if (StringUtils.isNotNull(parentUser.getParentUserId())) {
                sysUser.setTopUserId(parentUser.getParentUserId());
            }
        }
        sysUser.setCreateBy("admin");
        userService.insertUser(sysUser);
        //创建用户账户信息
        BizUserAccount bizUserAccount = new BizUserAccount();
        bizUserAccount.setUserId(sysUser.getUserId());
        bizUserAccount.setCreateTime(DateUtils.getNowDate());
        bizUserAccount.setUpdateTime(DateUtils.getNowDate());
        bizUserAccount.setCreateBy("admin");
        bizUserAccount.setUpdateBy("admin");
        bizUserAccountMapper.insert(bizUserAccount);
        //创建邀请记录
        if (StringUtils.isNotNull(registerFormVO.getInviteUserId())) {
            bizUserInviteRecordService.creatUserInviteRecord(registerFormVO.getInviteUserId(), sysUser.getUserId());
        }

        /**
         * 注册时同步向腾讯im 导入账号
         */
        TencentApiResponse apiResponse = liveApiService.accountImport(sysUser.getUserId().toString(), sysUser.getNickName(), sysUser.getAvatar());
        if (apiResponse.getCode() != 0) {
            throw new ApiException("注册腾讯云IM账号失败！ErrorInfo：" + apiResponse.getData());
        }
        //如果是医生，提前创建好直播房间
        if (registerFormVO.getClientType().equals("3")) {
            String userId = sysUser.getUserId().toString();
            BizLiveRoom bizLiveRoom = new BizLiveRoom();
            bizLiveRoom.setRoomId(userId);
            bizLiveRoom.setAnchorId(userId);
            bizLiveRoom.setCreateTime(DateUtils.getNowDate());
            bizLiveRoom.setCreateBy("admin");
            bizLiveRoom.setUpdateBy("admin");
            bizLiveRoom.insert();


            TencentApiResponse apiResponse2 = liveApiService.createGroup(userId);
            if (apiResponse2.getCode() == 10021 || apiResponse2.getCode() == 0) {
                return true;
            } else {
                throw new ApiException("创建直播群失败！ErrorInfo：" + apiResponse.getData());
            }
        }


        return true;
    }

    @Override
    public String appUserLogin(AppUserLoginFormVO loginFormVO) {
        if (!PhoneFormatCheckUtils.isChinaPhoneLegal(loginFormVO.getPhoneNumber())) {
            throw new ApiException("手机号码有误,请重新输入");
        }
        //SysUser user = userService.selectAppUserByPhone(loginFormVO.getPhoneNumber());
        SysUser user = null;
        if ("1".equals(loginFormVO.getClientType())) {
            user = userService.selectAppUserByPhoneAndType(loginFormVO.getPhoneNumber(), "10");
        } else if ("2".equals(loginFormVO.getClientType())) {
            user = userService.selectAppUserByPhoneAndType(loginFormVO.getPhoneNumber(), "20");
            if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
                user = userService.selectAppUserByPhoneAndType(loginFormVO.getPhoneNumber(), "30");
            }
            if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId())) {
                user = userService.selectAppUserByPhoneAndType(loginFormVO.getPhoneNumber(), "40");
            }
        } else if ("3".equals(loginFormVO.getClientType())) {
            user = userService.selectAppUserByPhoneAndType(loginFormVO.getPhoneNumber(), "50");
        }
        if (StringUtils.isNull(user)) {
            throw new ApiException("登录用户不存在，请注册用户，或者稍后再试");
        }
        if (StringUtils.equals(user.getDelFlag(), UserStatus.DELETED.getCode())) {
            throw new ApiException("登录用户：" + loginFormVO.getPhoneNumber() + "已被删除.");
        }
        if (StringUtils.equals(user.getStatus(), UserStatus.DISABLE.getCode())) {
            throw new ApiException("登录用户：" + loginFormVO.getPhoneNumber() + "已被停用.");
        }
        if (!StringUtils.equals(Constants.DEFAULT_LOGIN_CODE, loginFormVO.getCode())) {
            Object cacheObject = redisCache.getCacheObject(Constants.LOGIN_CACHE_PHONE_CODE_KEY + loginFormVO.getPhoneNumber());
            if (cacheObject == null) {
                throw new ApiException("短信验证码已过期");
            }
            String code = String.valueOf(cacheObject);
            if (!StringUtils.equals(code, loginFormVO.getCode())) {
                throw new ApiException("短信验证码输入错误");
            }
            redisCache.deleteObject(Constants.LOGIN_CACHE_PHONE_CODE_KEY + loginFormVO.getPhoneNumber());
        }
        LoginUser loginUser = new LoginUser(user, permissionService.getMenuPermission(user));
        String token = tokenService.createToken(loginUser);
        return token;
    }

    @Override
    public SysUser getUserInfo() {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        LambdaQueryWrapper<BizUserEnsureMoney> lambdaQueryWrapper = new LambdaQueryWrapper<BizUserEnsureMoney>()
                .eq(BizUserEnsureMoney::getUserId, userId)
                .orderByDesc(BizUserEnsureMoney::getCreateTime)
                .last(" limit 1");
        BizUserEnsureMoney money = BizUserEnsureMoney.DAO.selectOne(lambdaQueryWrapper);
        if (StringUtils.isNull(money)) {
            sysUser.setIsEarnestMoney("0");
            sysUser.setEarnestMoney(BigDecimal.ZERO);
        } else {
            if ("1".equals(money.getPayStatus()) || "2".equals(money.getPayStatus())) {
                sysUser.setIsEarnestMoney("1");
            } else {
                sysUser.setEarnestMoney(BigDecimal.ZERO);
                sysUser.setIsEarnestMoney("0");
            }
        }
        //如果是医生 查询医生的最新认证记录
        if (UserTypeEnum.DOCTOR.getCode().equals(sysUser.getUserType())) {
            AppDoctorProfessionalVO vo = bizUserProfessionRecordService.selectLastProfession(userId);
            if (StringUtils.isNull(vo)) {
                sysUser.setDoctorProfessionAuth("0");
                sysUser.setProfessionName("");
                sysUser.setHospitalName("");
                sysUser.setDepartmentName("");
            } else {
                sysUser.setDoctorProfessionAuth("1");
                sysUser.setProfessionName(vo.getProfessionName());
                sysUser.setHospitalName(vo.getHospitalName());
                sysUser.setDepartmentName(vo.getDepartmentName());
            }
        }

        String usersig = LiveUtils.genUsersig(String.valueOf(userId), liveProperties.getImSDKAppID(), liveProperties.getImSecretKey());
        sysUser.setUsersig(usersig);
        return sysUser;
    }

    @Override
    public Boolean submitIdCardInfo(AppSubmitIdCardInfoVo vo) {

        boolean flag = IdentificationCodeUtil.isIdentityCode(vo.getIdCardNumber());
        if (!flag) {
            throw new ApiException("身份证号码格式错误，请重新输入");
        }
        //判断有效日期是否合法 2018.10.12-2028.10.12
        Boolean validDate = validDate(vo.getIdCardValidDate());
        if (!validDate) {
            //判断有效日期是否合法 1970.10.12-长期
            if (vo.getIdCardValidDate().contains("长期")) {
                validDate = true;
            }
        }
        if (!validDate) {
            throw new ApiException("身份证有效期限格式输入错误，请严格参照身份证背面有效期限格式填写");
        }
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        //身份审核记录
        BizUserIdentityAuditRecord record = new BizUserIdentityAuditRecord();
        record.setUserId(userId);
        record.setUserType(sysUser.getUserType());
        record.setIdCardBack(vo.getIdCardBack());
        record.setIdCardFront(vo.getIdCardFront());
        record.setRealName(vo.getRealName());
        record.setIdCardNumber(vo.getIdCardNumber());
        record.setIdCardValidDate(vo.getIdCardValidDate());
        // 现在是默认认证成功
        record.setAuditStatus("1");
        boolean insert = record.insert();
        if (insert) {
            sysUser.setRealName(vo.getRealName());
            sysUser.setIdCardNumber(vo.getIdCardNumber());
            sysUser.setIdCardBack(vo.getIdCardBack());
            sysUser.setIdCardFront(vo.getIdCardFront());
            sysUser.setIdCardValidDate(vo.getIdCardValidDate());
            sysUser.setIsVerified(Constants.IS_VERIFIED_YES);
            // 现在是默认认证成功
            sysUser.setAuditStatus(Constants.AUDIT_STATUS_SUCCESS);
            sysUser.setAuditRemark("");
            int index = sysUserMapper.updateUser(sysUser);
        }
        return insert;
    }

    @Override
    public Boolean updateUserInfo(AppUpdateUserInfoVO userInfoVO) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        SysUser sysUser = sysUserMapper.selectUserById(user.getUserId());
        sysUser.setNickName(userInfoVO.getNickName());
        sysUser.setPhonenumber(userInfoVO.getPhoneNumber());
        sysUser.setUserName(userInfoVO.getPhoneNumber());
        sysUser.setAvatar(userInfoVO.getAvatar());
        sysUser.setIntroduction(userInfoVO.getIntroduction());
        sysUser.setUpdateBy(user.getUserName());
        int i = sysUserMapper.updateUser(sysUser);
        loginUser.setUser(sysUser);
        tokenService.refreshToken(loginUser);
        TencentApiResponse apiResponse = liveApiService.accountImport(sysUser.getUserId().toString(), sysUser.getNickName(), sysUser.getAvatar());
        if (apiResponse.getCode() != 0) {
            throw new ApiException("注册腾讯云IM账号失败！ErrorInfo：" + apiResponse.getData());
        }
        return i > 0;
    }

    @Override
    public Boolean updateUserPhone(AppUpdateUserPhoneVO vo) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        if (vo.getPhoneNumber().equals(user.getUserName())) {
            throw new ApiException("新旧手机号不能相同");
        }

        //int index = sysUserMapper.checkUserNameUnique(vo.getPhoneNumber());
        int index = 0;
        if (UserTypeEnum.MEMBER.getCode().equals(vo.getUserType()) || UserTypeEnum.DOCTOR.getCode().equals(vo.getUserType())) {
            index = sysUserMapper.checkUserNameAndTypeUnique(vo.getPhoneNumber(), vo.getUserType());
        } else {
            index = sysUserMapper.checkUserNameAndTypeUnique2(vo.getPhoneNumber());
        }
        if (index > 0) {
            throw new ApiException("该手机号已经被使用了");
        }
        if (!StringUtils.equals(Constants.DEFAULT_LOGIN_CODE, vo.getCode())) {
            Object cacheObject = redisCache.getCacheObject(Constants.UPDATE_CACHE_PHONE_CODE_KEY + vo.getPhoneNumber());
            if (cacheObject == null) {
                throw new ApiException("短信验证码已过期");
            }
            String code = String.valueOf(cacheObject);
            if (!StringUtils.equals(code, vo.getCode())) {
                throw new ApiException("短信验证码输入错误");
            }
        }
        redisCache.deleteObject(Constants.UPDATE_CACHE_PHONE_CODE_KEY + vo.getPhoneNumber());
        user.setUserName(vo.getPhoneNumber());
        user.setPhonenumber(vo.getPhoneNumber());
        user.setUpdateBy(user.getUserName());
        int i = sysUserMapper.updateUser(user);
        loginUser.setUser(user);
        tokenService.refreshToken(loginUser);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ApiAjaxResult submitJobAuth(AppSubmitJobAuthFormVo form) {
        if (!form.getProfessionName().equals("陪诊")) {
            if ("".equals(form.getValidDate())) {
                return ApiAjaxResult.success("有效期不能为空");
            }
            if ("".equals(form.getProfessionName())) {
                return ApiAjaxResult.success("职业名称不能为空");
            }
            if ("".equals(form.getProfessionImage())) {
                return ApiAjaxResult.success("职业认证图片不能为空");
            }
        }
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        BizUserProfessionRecord record = new BizUserProfessionRecord();
        QueryWrapper<BizUserProfessionRecord> queryWrapper = new QueryWrapper<>();
        QueryWrapper<BizUserProfessionRecord> user_id = queryWrapper.eq("user_id", userId);
        BizUserProfessionRecord bizUserProfessionRecord = record.selectOne(user_id);
        record.setUserId(userId);
        record.setUserType(sysUser.getUserType());
        record.setProfessionImage(form.getProfessionImage());
        record.setProfessionName(form.getProfessionName());
        if (form.getProfessionName().equals("陪诊")) {
            record.setAuditStatus(Constants.AUDIT_STATUS_SUCCESS);
            record.setStatus("1");
        } else {
            record.setAuditStatus(Constants.AUDIT_STATUS_WAIT);
        }
        record.setDepartmentId(form.getDepartmentId());
        record.setHospitalId(form.getHospitalId());
        if (StringUtils.isNotBlank(form.getValidDate())) {
            String[] dateArr = form.getValidDate().split("-");
            try {
                String start = dateArr[0].replace("/", "-");
                record.setValidStartDate(DateUtils.parseDate(start, "yyyy-MM-dd"));
                String end = dateArr[1].replace("/", "-");
                record.setValidEndDate(DateUtils.parseDate(end, "yyyy-MM-dd"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if (bizUserProfessionRecord != null) {
            if (bizUserProfessionRecord.getProfessionName().equals("医生")) {
                return ApiAjaxResult.success("申请成为医生已经成功,请等待系统审核");
            }
            if (bizUserProfessionRecord.getProfessionName().equals("护工")) {
                return ApiAjaxResult.success("申请成为" + bizUserProfessionRecord.getProfessionName() + "已经成功,无法申请护工");
            }
            if (bizUserProfessionRecord.getProfessionName().equals("陪诊")) {
                return ApiAjaxResult.success("申请成为" + bizUserProfessionRecord.getProfessionName() + "已经成功,无法申请护工");
            }
        } else {
            record.insert();
            return ApiAjaxResult.success("申请成为" + form.getProfessionName() + "已经成功,请等待系统审核");
        }
        return ApiAjaxResult.success();
    }

    @Override
    public AppUserAuthStatusVO selectAuthStatus() {
        AppUserAuthStatusVO statusVO = new AppUserAuthStatusVO();
        statusVO.setStatus(false);
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        if (Constants.IS_VERIFIED_YES.equals(sysUser.getIsVerified()) && Constants.AUDIT_STATUS_PASS.equals(sysUser.getAuditStatus())) {
            //查询保证金信息
            LambdaQueryWrapper<BizUserEnsureMoney> lambdaQueryWrapper = new LambdaQueryWrapper<BizUserEnsureMoney>()
                    .eq(BizUserEnsureMoney::getUserId, userId);
            BizUserEnsureMoney money = BizUserEnsureMoney.DAO.selectOne(lambdaQueryWrapper);
            if (money != null && (BizUserEnsureMoney.PAID.equals(money.getPayStatus()) || BizUserEnsureMoney.REFUNDING.equals(money.getPayStatus()))) {
                statusVO.setStatus(true);
            } else {
                statusVO.setType(2);
                throw new ApiException("您尚未缴纳保证金");
            }
        } else {
            statusVO.setType(1);
            throw new ApiException("您尚未完成实名认证");
        }
        return statusVO;
    }

    @Override
    public Boolean cancelIdCardAuth() {
        Boolean flag = false;
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        //查询当前用户的身份认证是第几次认证 1.第一次认证取消则是直接成为未实名 2.第二次或者更多次的认证取消后返回到上一个认证成功的身份
        LambdaQueryWrapper<BizUserIdentityAuditRecord> queryWrapper = new LambdaQueryWrapper<BizUserIdentityAuditRecord>()
                .eq(BizUserIdentityAuditRecord::getUserId, userId)
                .eq(BizUserIdentityAuditRecord::getAuditStatus, Constants.AUDIT_STATUS_PASS)
                .orderByDesc(BizUserIdentityAuditRecord::getCreateTime)
                .last(" limit 1");
        BizUserIdentityAuditRecord auditRecord = BizUserIdentityAuditRecord.DAO.selectOne(queryWrapper);
        SysUser updateUser = new SysUser();
        if (auditRecord == null) {
            //第一次认证取消则是直接成为未实名
            updateUser.setUserId(userId);
            updateUser.setIsVerified(Constants.IS_VERIFIED_NO);
            updateUser.setIdCardBack("");
            updateUser.setIdCardFront("");
            updateUser.setIdCardNumber("");
            updateUser.setRealName("");
            updateUser.setAuditStatus(Constants.AUDIT_STATUS_WAIT);
            updateUser.setIdCardValidDate("");
            updateUser.setAuditRemark("");
        } else {
            //2.第二次或者更多次的认证取消后返回到上一个认证成功的身份
            updateUser.setUserId(userId);
            updateUser.setIsVerified(Constants.IS_VERIFIED_YES);
            updateUser.setIdCardBack(auditRecord.getIdCardBack());
            updateUser.setIdCardFront(auditRecord.getIdCardFront());
            updateUser.setIdCardNumber(auditRecord.getIdCardNumber());
            updateUser.setRealName(auditRecord.getRealName());
            updateUser.setAuditStatus(Constants.AUDIT_STATUS_SUCCESS);
            updateUser.setIdCardValidDate(auditRecord.getIdCardValidDate());
            updateUser.setAuditRemark(auditRecord.getAuditRemark());
        }
        return sysUserMapper.updateUser(updateUser) > 0;
    }

    @Override
    public List<AppUserResultVO> selectAppUserList(String phone) {
        List<AppUserResultVO> resultVOList = new ArrayList<>();
        resultVOList = sysUserMapper.selectAppUserList(phone);
        return resultVOList;
    }

    @Override
    public ApiAjaxResult getAllStatus() {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        HashMap hashMap = new HashMap();
        // 用户是否实名认证
        boolean auditStatus = sysUser.getAuditStatus().equals("1") ? true : false;
        BizUserProfessionRecord record = new BizUserProfessionRecord();
        QueryWrapper<BizUserProfessionRecord> queryWrapper = new QueryWrapper<>();
        QueryWrapper<BizUserProfessionRecord> user_id = queryWrapper.eq("user_id", userId);
        List<BizUserProfessionRecord> bizUserProfessionRecords = record.selectList(user_id);
        hashMap.put("idCard", auditStatus);
        if (auditStatus) {
            // bizUserProfessionRecords.size() == 0 设置默认值
            hashMap.put("accompanyingStatus", false);
            hashMap.put("companionStatus", false);
            hashMap.put("doctorStatus", false);
            for (int i = 0; i < bizUserProfessionRecords.size(); i++) {
                if (bizUserProfessionRecords.get(i).getProfessionName().equals("医生")) {
                    if (bizUserProfessionRecords.get(i).getAuditStatus().equals("1")) {
                        hashMap.put("doctorStatus", true);
                        hashMap.put("accompanyingStatus", false);
                        hashMap.put("companionStatus", false);
                    } else {
                        hashMap.put("doctorStatus", false);
                        hashMap.put("accompanyingStatus", false);
                        hashMap.put("companionStatus", false);
                    }
                } else {
                    if (bizUserProfessionRecords.get(i).getProfessionName().equals("护工")) {
                        if (bizUserProfessionRecords.get(i).getAuditStatus().equals("1")) {
                            hashMap.put("companionStatus", true);
                        } else {
                            hashMap.put("companionStatus", false);
                        }
                        hashMap.put("accompanyingStatus", true);
                        hashMap.put("doctorStatus", false);
                    } else {
                        hashMap.put("accompanyingStatus", true);
                        hashMap.put("companionStatus", false);
                        hashMap.put("doctorStatus", false);
                    }
                }
            }
        } else {
            hashMap.put("accompanyingStatus", false);
            hashMap.put("companionStatus", false);
            hashMap.put("doctorStatus", false);
        }
        return ApiAjaxResult.success(hashMap);
    }

    public Boolean validDate(String dateStr) {
        Boolean flag = true;
        if (dateStr.length() < 11) {
            return false;
        }
        String str = String.valueOf(dateStr.charAt(10));
        if (StringUtils.equals(str, "-")) {
            String[] dateArr = dateStr.split("-");
            SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
            format.setLenient(false);
            try {
                Date date1 = format.parse(dateArr[0]);
                Date date2 = format.parse(dateArr[1]);
                if (date1.after(date2)) {
                    return false;
                }
            } catch (Exception e) {
                flag = false;
            }

        } else {
            return false;
        }
        return flag;
    }
}
